/*
 * Copyright 2014 Jens K., Jalbum AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.package net.jalbum.jprint;
 */

package net.jalbum.areatagging;

import com.adobe.xmp.XMPException;
import com.adobe.xmp.XMPMeta;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;

/**
 * An instance of a single area tag (not specified).
 */
public abstract class AreaTag {
    private final String unit = "normalized";
    private String description, name, type;
    private float x, y;
    
    // draw variables
    private static final int borderInactive = 3;                               // border width of the inactive tag(s)
    private static final int borderActive = 8;                                 // border width of the active tag
    private static final int space = 2;                                        // space around the name area
    private static final int shadow = 1;                                       // shadow inside a tag
    private static final Font font = new Font("SansSerif", Font.PLAIN, 12);    // font for the tag names on image
    private static final Color dimmedW = new Color(255, 255, 255, 180);        // dimmed white color
    private static final Color dimmedG = new Color(220, 220, 220, 180);        // dimmed gray color
    private static final Color dimmedBsoft = new Color(40, 40, 40, 120);       // dimmed black color (low alpha)
    private static final Color dimmedBhard = new Color(40, 40, 40, 220);       // dimmed black color (high alpha)

    public String getName() {
        return name;
    }
    
    public String getDescription() {
        return description;
    }

    public String getType() {
        return type;
    }

    public String getUnit() {
        return unit;
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public int getBorderInactive() {
        return borderInactive;
    }

    public int getBorderActive() {
        return borderActive;
    }

    public int getSpace() {
        return space;
    }

    public Font getFont() {
        return font;
    }

    public Color getDimmedW() {
        return dimmedW;
    }

    public Color getDimmedG() {
        return dimmedG;
    }

    public Color getDimmedBsoft() {
        return dimmedBsoft;
    }

    public Color getDimmedBhard() {
        return dimmedBhard;
    }

    public int getShadow() {
        return shadow;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setX(float x) {
        this.x = x;
    }
    
    public void setY(float y) {
        this.y = y;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        
        AreaTag tag = (AreaTag)obj;
        int xa = Math.round(x * 100);
        int xb = Math.round(tag.getX() * 100);
        int ya = Math.round(y * 100);
        int yb = Math.round(tag.getY() * 100);
        
        return (xa == xb) && (ya == yb);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 29 * hash + Float.floatToIntBits(this.x);
        hash = 29 * hash + Float.floatToIntBits(this.y);
        return hash;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    
    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder();
        buf.append("\ttag name: \t\t ").append(name).append("\n");
        buf.append("\ttag description: \t ").append(description).append("\n");
        buf.append("\ttag type: \t\t ").append(type).append("\n");
        buf.append("\ttag unit: \t\t ").append(unit).append("\n");
        buf.append("\tx-position of tag: \t ").append(x).append("\n");
        buf.append("\ty-position of tag: \t ").append(y).append("\n");
        
        return buf.toString();
    }
    
    public float capValue(float value, float min, float max) {
        value = Math.max(value, min);
        value = Math.min(value, max);
        return value;
    }
    
    abstract void drawInactive(Graphics2D big, Dimension sDim);
    
    abstract void drawActive(Graphics2D big, Dimension sDim, String name);
    
    abstract int resizeOrMove(MouseState state, Dimension sDim, Point p);
    
    abstract MouseState updateMouseState(Graphics2D g, Dimension sDim, Point p);
    
    abstract boolean isPointInsideTag(Graphics2D g, Dimension dim, Point p);
    
    abstract boolean isPointInsideName(Graphics2D g, Dimension dim, Point p);
    
    abstract void storeMWGAreaTags(XMPMeta meta, int item) throws XMPException;
    
    abstract void storeMSAreaTags(XMPMeta meta, int item, Dimension dim) throws XMPException;
}
